//
//  STSellerViewController.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STSellerViewController.h"
#import "STRefreshTableView.h"
#import "STSellerBean.h"
#import "STCommodityBean.h"
#import "STCommodityDetailViewController.h"
#import "STSellerDetailViewController.h"
#import "STAreasTitleView.h"
#import "STTitleViewLayout.h"
#import "STAreaBean.h"
#import "STSellerBean.h"
@interface STSellerViewController ()<STBaseViewDelegate>

@property (nonatomic ,strong) STRefreshTableView *tableView;

@property (nonatomic ,copy) NSString *page;

@property (nonatomic ,copy) NSString *systemTime;

@property (nonatomic ,strong) STAreasTitleView *titleView;

@property (nonatomic ,strong) STAreaBean *area;
@end

@implementation STSellerViewController

- (void)initSubviews {
    [super initSubviews];
    
    self.automaticallyAdjustsScrollViewInsets = false;
    
    [self initHeader];
    
    [self initTableView];
}
- (void)initTableView {
    
    STRefreshTableView *tableView = [STRefreshTableView instance];
    
    self.tableView = tableView;
    
    tableView.cellType = STCellTypeSellerList;
    
    tableView.mDelegate = self;
    
    [self.view addSubview:tableView];
    
    [tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.top.equalTo(30);
        make.bottom.equalTo(0);
    }];
}

- (void)initHeader {
    
    STAreasTitleView *titleView = [STAreasTitleView instanceWithFrame:CGRectMake(0, 0, ST_SCREEN_WIDTH - 30, 30)];
    
    self.titleView = titleView;
    
    titleView.mDelegate = self;
    
    [self.view addSubview:titleView];
    
    UIButton *showArea = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [showArea setImage:[UIImage imageNamed:@"添加－30"] forState:UIControlStateNormal];
    
    [showArea setImage:[UIImage imageNamed:@"添加－30"] forState:UIControlStateHighlighted];
    
    [self.view addSubview:showArea];
    
    showArea.backgroundColor = [UIColor redColor];
    
    [showArea addTarget:self action:@selector(showAreaTriggered) forControlEvents:UIControlEventTouchUpInside];
    [showArea makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(0);
        make.top.equalTo(0);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
}
- (void)loadData {
    
    [self.titleView updateHeader:@"常用地区"];
    
    id areaData = [[STCacheDataManager getInstance] queryDefaultAreasListData];
    
    if (areaData) {
        
        [self.titleView updateData:areaData];
    }
    
    id sellers = [[STCacheDataManager getInstance] querySellerDataForKey:ST_Total_Seller_Cache];
    
    if (sellers) {
        
        [self.tableView firstLoadData:sellers];
        
    }
    @WeakObj(self)
    [[STHttpManager getInstance] requestWithUrlString:ST_HTTP_GETCOMMONAREAS andParams:@{} andStartBlock:^BOOL{
        return true;
    } andSuccessBlock:^(NSString *rescode, NSString *msg, NSString *token, NSString *sysTime, id data, NSString *page) {
        if ([rescode isEqualToString:@"100"]) {
            NSArray *areas = [STAreaBean mj_objectArrayWithKeyValuesArray:data];
            
            [[STCacheDataManager getInstance] saveDefaultAreasListData:areas];
            
            [selfWeak.titleView updateData:areas];
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:msg];
            
        }
    } andFailureBlock:^BOOL{
        
        return true;
    }];
}
- (void)loadNewData {
    
    [self loadNetData:true];
    
}
- (void)loadMoreData {
    
    [self.tableView.mj_footer beginRefreshing];
    
    [self loadNetData:false];
    
}
// 如果isnew true  下拉刷新  如果 isnew false 上拉加载
- (void)loadNetData:(BOOL )isNew {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    @WeakObj(self)
    
    if (self.area) {
        
        [dict setObject:self.area.aid forKey:@"areaId"];
    }
    if (isNew) {
        
        self.page = @"0";
        
    } else {
        
        NSInteger currentPage = [self.page integerValue];
        
        currentPage += 1;
        
        self.page = [NSString stringWithFormat:@"%ld",currentPage];
        
    }
    [dict setObject:self.page forKey:@"page"];
    
    [[STHttpManager getInstance] requestWithUrlString:ST_HTTP_GETSELLER andParams:dict andStartBlock:^BOOL{
        return  true;
    } andSuccessBlock:^(NSString *rescode, NSString *msg, NSString *token, NSString *sysTime, id data, NSString *page) {
        if ([rescode isEqualToString:@"100"]) {
            selfWeak.page = page;
            
            selfWeak.systemTime = sysTime;
            //
            NSArray *sellers =  [STSellerBean mj_objectArrayWithKeyValuesArray:data];
            if (selfWeak.area) {
                
                [[STCacheDataManager getInstance] saveSellerData:sellers forKey:selfWeak.area.aid];
                
            } else {
                
                [[STCacheDataManager getInstance] saveSellerData:sellers forKey:ST_Total_Seller_Cache];
            }
            
            if (isNew) {
                
                [selfWeak.tableView updateData:sellers];
                
            } else {
                
                if (sellers.count) {
                    
                    [selfWeak.tableView appendData:sellers];
                    
                } else {
                    
                    selfWeak.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                }
            }
        } else {
            
            [SVProgressHUD showInfoWithStatus:msg];
            
        }
        if (isNew) {
            
            [selfWeak.tableView.mj_header endRefreshing];
            
        } else {
            
            [selfWeak.tableView.mj_footer endRefreshing];
        }
    } andFailureBlock:^BOOL{
        
        if (isNew) {
            
            [selfWeak.tableView.mj_header endRefreshing];
            
        } else {
            
            [selfWeak.tableView.mj_footer endRefreshing];
        }
        return true;
    }];
    
}
- (void)showAreaTriggered {
    
    [self onItemClick:STItemClickTypeShowArea withData:nil forIndexPath:nil];
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    switch (itemType) {
        case STItemClickTypeItem:
        {
            STSellerDetailViewController *sellerDetail = [STSellerDetailViewController instance];
            
            [self.navigationController pushViewController:sellerDetail animated:true];
        }
            break;
        case STItemClickTypeGoods:
        {
            STCommodityDetailViewController *goods = [STCommodityDetailViewController instance];
            
            [self.navigationController pushViewController:goods animated:true];
            
        }
            break;
            
        case STItemClickTypeArea:
        {
            self.area = data;
            
            id sellers = [[STCacheDataManager getInstance] querySellerDataForKey:self.area.aid];
            
            [self.tableView updateData:sellers];
            
            [self.tableView.mj_header beginRefreshing];
            
            //            [self loadNetData:true];
        }
            break;
        case STItemClickTypeShowArea:
        {
            
            
        }
            break;
        case  STItemClickTypeTotalArea:
        {
            self.area = nil;
            
            id sellers = [[STCacheDataManager getInstance] querySellerDataForKey:ST_Total_Seller_Cache];
            
            if (sellers) {
                
                [self.tableView updateData:sellers];
                
            }
            [self.tableView.mj_header beginRefreshing];
            
        }
            break;
            
//        case STItemClickTypeScroll:
//        {
//            
//        }
//            break;
        default:
            break;
    }
}

@end
